// antd 按需加载
import { Toast, Portal } from '@ant-design/react-native';

import store from '../../../store/store.js'
import * as ActionTypes from '../../../constants/constants.js'
import { isUser, isWxUser, path, saveData, loadData, removeData, requestMessage } from '../../../util/utils.js'
import request from '../../../api/request.js'

export async function getData(options, callback) {
    const { dispatch } = this.props
    const { refresh } = options
    const loading = Toast.loading('加载中', 0, null, false)
    const attr = await request.GoodsController.attr(options)
    if (attr.error) {
        Portal.remove(loading)
        dispatch({
            type: ActionTypes.goodsAttrError,
            data: attr
        })
    } else {
        dispatch({
            type: ActionTypes.goodsAttr,
            data: attr.data.data || null
        })
    }
    const details = await request.GoodsController.details(options)
    if (details.error) {
        Portal.remove(loading)
        dispatch({
            type: ActionTypes.goodsDetailsError,
            data: details
        })
    } else {
        dispatch({
            type: ActionTypes.goodsDetails,
            data: details.data.data || null
        })
    }
    Portal.remove(loading)
    callback()
}

export async function addShopCart(options) {
    const loading = Toast.loading('加入购物车中', 0, null, false)
    const res = await request.CartController.add(options)
    return requestMessage(res, loading, {
        success: '已加入购物车',
        error: res.data.message
    })
}



// export async function details(options) {
//     const { dispatch } = this.props
//     const { refresh } = options
//     const res = await request.GoodsController.details(options)
//     if (res.error) {
//         dispatch({
//             type: ActionTypes.goodsDetailsError,
//             data: res
//         })
//         return
//     }
//     dispatch({
//         type: ActionTypes.goodsDetails,
//         data: res.data.data
//     })
// }

// export async function attr(options) {
//     const { dispatch } = this.props
//     const { refresh } = options
//     const res = await request.GoodsController.attr(options)
//     if (res.error) {
//         dispatch({
//             type: ActionTypes.goodsAttrError,
//             data: res
//         })
//         return
//     }
//     dispatch({
//         type: ActionTypes.goodsAttr,
//         data: res.data.data
//     })
// }




